package com.gm.blog.controller;

import com.gm.blog.enums.ResultEnum;
import com.gm.blog.pojo.Type;
import com.gm.blog.service.TypeService;
import com.gm.blog.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类模块管理
 * @author 29944
 */
@RequestMapping("/type")
@RestController
public class TypeController {
    @Autowired
    private TypeService typeService;

    /**
     * 添加类型
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Result<Object> save(@RequestBody Type type){
        typeService.insert(type);
        return new Result<>("添加成功");
    }

    /**
     * 查询type列表
     * @return
     */
    @RequestMapping(value="/list",method = RequestMethod.GET)
    public Result<Object> list(){
        List<Type> typeAll = typeService.getTypeAll();
        return new Result<>("查询成功",typeAll);
    }
    /**
     * 前台查询所有分类
     * @return
     */
    @RequestMapping(value = "/fontList",method = RequestMethod.GET)
    public Result<Object> fontList(){
        List<Type> typeAll=typeService.getFontAll();
        return new Result<>("查询成功",typeAll);
    }

    /**
     * 修改分类数据
     */
    @RequestMapping(value="/update",method=RequestMethod.POST)
    public Result<Object> updateType(@RequestBody Type type){
        typeService.update(type);
        return new Result<>(ResultEnum.SUCCESS);
    }
    /**
     * 更具ID启用数据
     */
    @RequestMapping(value="/enable/{id}",method = RequestMethod.PUT)
    public Result<Object> enable(@PathVariable Integer id){
        Type typeById = typeService.getTypeById(id);
        if(typeById==null){
            return new Result<>(ResultEnum.DATA_NOT_FOUND);
        }else{
            typeService.enabledById(id);
            return new Result<>(ResultEnum.SUCCESS);
        }

    }
    /**
     * 更具ID弃用数据
     */
    @RequestMapping(value = "/disable/{id}",method = RequestMethod.PUT)
    public Result<Object> disable(@PathVariable Integer id){
        Type typeById = typeService.getTypeById(id);
        if(typeById==null){
            return new Result<>(ResultEnum.DATA_NOT_FOUND);
        }else{
            typeService.disabledById(id);
            return new Result<>(ResultEnum.SUCCESS);
        }
    }
    /**
     * 更具ID删除分类
     */
    @RequestMapping(value = "/deleted/{id}",method = RequestMethod.DELETE)
    public Result<Object> delete(@PathVariable Integer id){
        Type typeById = typeService.getTypeById(id);
        if(typeById==null){
            return new Result<>(ResultEnum.DATA_NOT_FOUND);
        }else{
            typeService.delTypeById(id);
            return new Result<>(ResultEnum.SUCCESS);
        }
    }
}
